package com.atguigu.interview.study.juc;

import java.util.concurrent.TimeUnit;

/**
 * @author gcq
 * @Create 2020-12-07
 * 要求t1线程等待3秒钟，3秒钟后t2线程唤醒t1线程继续工作
 * 3、将notify放在wait方法前执行，t1先notify，3秒钟后t2线程再执行wait方法
 *  3.1 程序无法结束
 *  3.2 结论
 *  先wait然后再notify，等待中的线程才会被唤醒，否则无法唤醒
 *
 */
public class LockSupportDemo2 {
    static Object objectLock = new Object();

    public static void main(String[] args) {

        new Thread(() ->{
            synchronized (objectLock) {
                objectLock.notify();
                System.out.println(Thread.currentThread().getName() + "\t" + "----通知");
            }
        },"t1").start();

        new Thread(() -> {
            synchronized (objectLock) {
            // 暂停一会线程
            try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
                System.out.println(Thread.currentThread().getName() + "\t" + "-----------come in");
                try {
                    objectLock.wait();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "\t" + "---被唤醒");
            }
        },"A").start();

    }
}